package com.mcxx.modules.miniSalvation.fundpay.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.CustomFundPayReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayExcelDto;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayExcelDto;
import com.mcxx.modules.miniSalvation.fundpay.entity.CustomFundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.CustomFundPayListSearchParam;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("customFundPayReadService")
@TaTransactional(readOnly = true)
public class CustomFundPayReadService  extends BaseService {
    @Autowired
    private CustomFundPayReadDao customFundPayReadDao;


    public Page getCustomFundPayList(CustomFundPayListSearchParam customFundPayListSearchParam, PageParam pageParam, UserAccountVo userAccountVo){

        QueryParamUtil.setBaseParamValue(customFundPayListSearchParam, userAccountVo);

        customFundPayReadDao.beginPager(pageParam);
        List<CustomFundPayEntity> fundPayEntityList = customFundPayReadDao.getCustomFundPayList(customFundPayListSearchParam);
        return customFundPayReadDao.endPager(fundPayEntityList);
    }


    public int cntPayFundCount(CustomFundPayDTO customFundPayDTO){
        int rst = customFundPayReadDao.cntPayFundCount(customFundPayDTO);
        if(rst > 0){
            throw new BizException("-1", "名册已经创建");
        }
        return rst;
    }



    public Map<String, List<String>> getIdByIdAndStates(List<String> fundPayIdList, List<String> stateList){
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> canDelIdList = customFundPayReadDao.getIdByIdAndStates(fundPayIdList, stateList);
        if(canDelIdList.size() == fundPayIdList.size()){

            rstMap.put("success", canDelIdList);
            return rstMap;
        }
        if(canDelIdList.size() == 0){

            rstMap.put("fail", fundPayIdList);
            return rstMap;
        }

        List<String> canNotDelIdList = fundPayIdList.stream().filter(id -> canDelIdList.contains(id) == false).collect(Collectors.toList());
        rstMap.put("success", canDelIdList);
        rstMap.put("fail", canNotDelIdList);
        return rstMap;
    }



    public Map<String, List<String>> getIdByIdAndState(List<String> fundPayIdList, String state){
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> successIdList = customFundPayReadDao.getIdByIdAndState(fundPayIdList, state);
        if(successIdList.size() == fundPayIdList.size()){

            rstMap.put("success", successIdList);
            return rstMap;
        }
        if(successIdList.size() == 0){

            rstMap.put("fail", fundPayIdList);
            return rstMap;
        }

        List<String> failIdList = fundPayIdList.stream().filter(id -> successIdList.contains(id) == false).collect(Collectors.toList());
        rstMap.put("success", successIdList);
        rstMap.put("fail", failIdList);
        return rstMap;
    }



    public Map<String, List<String>> getIdByIdAndMemberNum(List<String> customFundPayIdList){

        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> haveMemberFundPayIdList = customFundPayReadDao.getIdByIdAndMemberNum(customFundPayIdList);
        if(haveMemberFundPayIdList.size() <= 0){

            rstMap.put("fail", customFundPayIdList);
            return rstMap;
        }
        List<String> noMemberFundPayIdList = customFundPayIdList.stream().filter(id -> haveMemberFundPayIdList.contains(id) == false).collect(Collectors.toList());
        if(haveMemberFundPayIdList.size() > 0){
            rstMap.put("success", haveMemberFundPayIdList);
        }
        if(noMemberFundPayIdList.size() > 0){
            rstMap.put("fail", noMemberFundPayIdList);
        }
        return rstMap;
    }


    public CustomFundPayDTO getById(String id){
        CustomFundPayDTO customFundPayDTO = customFundPayReadDao.getById(id);
        if(null == customFundPayDTO){
            throw new BizException(FailureCode.ERR_10042);
        }
        return customFundPayDTO;
    }


    public Integer checkAllowanceCanAddMember(String fundPayId){
        return customFundPayReadDao.checkAllowanceCanAddMember(fundPayId);
    }


    public List<CustomFundPayExcelDto> exportExcelCustFundPay(CustomFundPayListSearchParam customFundPayListSearchParam){ return customFundPayReadDao.exportExcelCustFundPay(customFundPayListSearchParam);}

}
